<?php
	session_unset();
	session_start();
	require_once('../inc/fnc.php');
	require_once('../inc/conn.php');
	require_once('../inc/settings.php');

	//Set values
	$fbid = $_GET['id'];
	$email = $_GET['email'];
	$name = $_GET['name'];
	$fname = $_GET['fname'];
	$lname = $_GET['lname'];
	$username = $_GET['username'];
	$type = $_GET['type'];
	
	//Check if the user is already registered using FB
	$query = "SELECT `userid` FROM `users` WHERE `fbuid` = {$fbid} AND `delete flag` = 'N' LIMIT 1";
	$result = mysql_query($query, $connection);

	if(mysql_num_rows($result) == 1){
		//User already registered through FB. Now log in user
		$_SESSION['login'] = TRUE;
		
		while($row = mysql_fetch_array($result)){
			$_SESSION['user'] = getUsername($row['userid']);
			$_SESSION['userId'] = $row['userid'];
		}
		$return['status'] = 'login';
	} else {
		//Check if user's email is registered with NT
		$query = "SELECT `userid` FROM `users` WHERE `email` = '{$email}' AND `delete flag` = 'N' LIMIT 1";
		$resultset = mysql_query($query, $connection);

		if(mysql_num_rows($resultset) == 1){
			//User's email already registered, prompt to merge account with FB
			$return['status'] = 'merge';
		} else {
			//User not registered, register anew
			//If true, user has not set username in FB, so assign one here.
			if(strlen($username) < 1){
				$return['status'] = 'username';
			} else {
			
				//If true, username available
				if(!checkavailable($name)){
					$return['status'] = 'unavailable';
				} else {

					$userId = getnewuserid();
					$time = time();

					$dirTime = date('MY',time());
					$imagepath = "users/".$dirTime.'/'.$userId;

					//Create directory for profile image upload if it doesn't exist already
					list($part1, $part2, $part3) = explode("/", $imagepath);
					if(!is_dir('../' . $part1."/".$part2)){
						if (!mkdir('../' . $part1."/".$part2, 0777)) {
							 die('Failed to create folders... 1');
						}
						if (!mkdir('../' . $part1."/".$part2."/".$part3, 0777)) {
							 die('Failed to create folders... 2');
						}
						
					} elseif(!is_dir('../' . $part1."/".$part2."/".$part3)){
						if (!mkdir('../' . $part1."/".$part2."/".$part3, 0777)) {
							 die('Failed to create folders... 3');
						}
					}
					
					$copyTo = '../' . $imagepath . '/' . $userId . '.jpg';
					$copyFrom = 'http://graph.facebook.com/' . $fbid . '/picture?type=large';
					if(copy($copyFrom, $copyTo)){
						crop($copyTo, '../' . $imagepath.'/small', 40, 40);
						crop($copyTo, '../' . $imagepath.'/dp', 150, 150);
					} else {
						//"Copy Fail";
						$imagepath = 'users/defaultAvatar/';
					}
					//Register new user
					$query = "INSERT INTO `users` (`userid`,`fbuid`, `username`, `email`,`profile image path`, `time`, `isadmin`,`delete flag`,`email varification`) 
									VALUES ('{$userId}', {$fbid}, '{$username}', '{$email}', '{$imagepath}', '{$time}', 'N','N','Y')";
					$result = mysql_query($query, $connection);
					
					//now log in user
					$_SESSION['login'] = TRUE;
					$_SESSION['user'] = $username;
					$_SESSION['userId'] = $userId;
					$return['status'] = 'register';
					$return['uid'] = getUserId($email);
				}
			}
		}
	}

	echo json_encode($return);

?>